package com.github.utils;


import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.Collection;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * 权限相关工具类
 *
 * @author Mr.xie
 */
public class PermissionUtils {


    private final int REQUEST_CODE = 1000;

    public static interface IPermissionCallBack {

        /**
         * 被授权
         */
        void granded();


        /**
         * 授权失败回调
         */

        void error();


        /**
         * 授权成功 带有权限名称
         * @param grandedPermissionName  授权成功的权限名称
         */

        void grandedWithPermission(String grandedPermissionName);

        /**
         * 授权失败 带有权限名称
         * @param grandedPermissionName  授权失败的权限名称
         */

        void errorWithPermission(String grandedPermissionName);


        /**
         * 在授权之前的调用操作
         */
        void requestAfter();

    }


    private static PermissionUtils permissionUtils = null;


    private static AppCompatActivity mActivity = null;


    private static ActivityResultLauncher<String> launcher = null;

    private static ActivityResultLauncher<String[]> mutLauncher = null;

    private static IPermissionCallBack iPermissionCallBack = null;

    public static void init(AppCompatActivity activity) {
        if (activity != null) {
            mActivity = activity;
            launcher = activity.registerForActivityResult(new ActivityResultContracts.RequestPermission(), activityResultCallback);
            mutLauncher = activity.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), mutResultCallBack);
        }

    }


    /**
     * 单个权限请求的回调接口
     */
    private static ActivityResultCallback<Boolean> activityResultCallback = new ActivityResultCallback<Boolean>() {
        @Override
        public void onActivityResult(Boolean result) {
            if (iPermissionCallBack == null) {
                iPermissionCallBack.error();
                return;
            }
            if (result) {
                iPermissionCallBack.granded();
            } else {
                iPermissionCallBack.error();
            }
        }
    };


    /**
     * 多个权限请求的回调接口
     */
    private static ActivityResultCallback mutResultCallBack = new ActivityResultCallback<Map<String, Boolean>>() {
        boolean allGranted = true;

        @Override
        public void onActivityResult(Map<String, Boolean> result) {

            result.forEach((perms, grand) -> {
                if (!grand) {
                    allGranted = false;
                    iPermissionCallBack.errorWithPermission(perms);
                } else {
                    iPermissionCallBack.grandedWithPermission(perms);
                }
            });
            if (allGranted) iPermissionCallBack.granded();
            else iPermissionCallBack.error();
        }
    };


    public static PermissionUtils getInstance() {
        synchronized (PermissionUtils.class) {
            if (permissionUtils == null) {
                permissionUtils = new PermissionUtils();
            }
            return permissionUtils;
        }
    }


    public void requestPermission(String permissions, IPermissionCallBack callBack) {
        if (callBack == null) {
            throw new IllegalArgumentException("权限回调接口是必须传入的 IPermissionCallBack ");
        }
        if (launcher == null) {
            callBack.error();
            return;
        }
        iPermissionCallBack = callBack;
        iPermissionCallBack.requestAfter();
        if (ActivityCompat.checkSelfPermission(mActivity, permissions) == PackageManager.PERMISSION_GRANTED) {
            callBack.granded();
            return;
        }
        launcher.launch(permissions);
    }

    /**
     * 请求多个权限
     * @param permissions  多个参数
     * @param callBack  回调接口
     */
    public void requestPermissions(IPermissionCallBack callBack, String... permissions) {
        if (callBack == null) {
            throw new IllegalArgumentException("权限回调接口是必须传入的 IPermissionCallBack ");
        }
        if (launcher == null) {
            callBack.error();
            return;
        }
        iPermissionCallBack = callBack;
        iPermissionCallBack.requestAfter();
        mutLauncher.launch(permissions);
    }


    /**
     * 请求多个权限
     * @param permissions  权限数组
     * @param callBack  回调接口
     */
    public void requestPermissions(String[] permissions, IPermissionCallBack callBack) {
        if (callBack == null) {
            throw new IllegalArgumentException("权限回调接口是必须传入的 IPermissionCallBack ");
        }
        if (launcher == null) {
            callBack.error();
            return;
        }
        iPermissionCallBack = callBack;
        iPermissionCallBack.requestAfter();
        mutLauncher.launch(permissions);
    }


    /**
     * 请求多个权限
     * @param permissions  权限集合
     * @param callBack  回调接口
     */
    public void requestPermissions(Collection<String> permissions, IPermissionCallBack callBack) {
        if (callBack == null) {
            throw new IllegalArgumentException("权限回调接口是必须传入的 IPermissionCallBack ");
        }
        if (launcher == null) {
            callBack.error();
            return;
        }
        iPermissionCallBack = callBack;
        iPermissionCallBack.requestAfter();
        mutLauncher.launch(permissions.toArray(new String[]{}));
    }


}
